home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Browsers, Managers & Extensions / Mozilla Weave 0.2.7 / latest-weave.xpi / chrome / sync.jar / content / oauth.js < prev    next >
Text File  |  2008-08-08  |  9KB  |  245 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is Bookmarks Sync.
  15.  *
  16.  * The Initial Developer of the Original Code is Mozilla.
  17.  * Portions created by the Initial Developer are Copyright (C) 2008
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *  Anant Narayanan <anant@kix.in>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. function OAuthWizard() {
  38.   this._init();
  39. }
  40. OAuthWizard.prototype = {
  41.   __os: null,
  42.   get _os() {
  43.     if (!this.__os)
  44.       this.__os = Components.classes["@mozilla.org/observer-service;1"]
  45.                   .getService(Components.interfaces.nsIObserverService);
  46.     return this.__os;
  47.   },
  48.  
  49.   get _stringBundle() {
  50.     let stringBundle = document.getElementById('weaveStringBundle');
  51.     this.__defineGetter__("_stringBundle", function() {return stringBundle;});
  52.  
  53.     return this._stringBundle;
  54.   },
  55.  
  56.   _init: function OAuthWizard__init() {
  57.     this._log = Log4Moz.Service.getLogger("Chrome.OAuthWizard");
  58.     this._log.info("Intializing OAuth wizard");
  59.     
  60.     this._os.addObserver(this, "weave:service:verify-passphrase:success", false);
  61.     this._os.addObserver(this, "weave:service:login:success", false);
  62.     this._os.addObserver(this, "weave:service:verify-passphrase:error", false);
  63.     this._os.addObserver(this, "weave:service:login:error", false);
  64.   },
  65.  
  66.   uninit: function OAuthWizard_uninit() {
  67.     this._log.info("Shutting down OAuth wizard");
  68.  
  69.     this._os.removeObserver(this, "weave:service:passphrase:success");
  70.     this._os.removeObserver(this, "weave:service:passphrase:error");
  71.   },
  72.  
  73.   enableAuth: function OAuthWizard_enableAuth() {
  74.     let aubut = document.getElementById('oauth-intro-aubut');
  75.     let pass = document.getElementById('oauth-intro-phrase');
  76.     aubut.disabled = true;
  77.  
  78.     if (!Weave.Service.isLoggedIn) {
  79.       let uid = document.getElementById('oauth-intro-uid');
  80.       let pwd = document.getElementById('oauth-intro-pwd');
  81.  
  82.       aubut.disabled = (uid.value.length == 0 || uid.value.length == 0 || pass.value.length == 0);
  83.     } else {
  84.       aubut.disabled = (pass.value.length == 0);
  85.     }
  86.   },
  87.   
  88.   checkLogin: function OAuthWizard_checkLogin() {
  89.     document.getElementById('oauth-wizard').canAdvance = false;
  90.     document.getElementById('oauth-intro-aubut').disabled = true;
  91.     if (!Weave.Service.isLoggedIn) {
  92.       let upbox = document.getElementById('oauth-intro-upbox');
  93.       upbox.hidden = false;
  94.     } else {
  95.       let sbox = document.getElementById('oauth-intro-sin');
  96.       let unam = document.getElementById('oauth-intro-pmsg');
  97.  
  98.       unam.value = this._stringBundle.getFormattedString('intro.uidmsg', [Weave.Service.username]);
  99.       sbox.hidden = false;
  100.     }
  101.   },
  102.   
  103.   verifyLogin: function OAuthWizard_verifyLogin() {
  104.     let aubox = document.getElementById('oauth-intro-aubox');
  105.     let sibox = document.getElementById('oauth-intro-athing');
  106.     let ssbox = document.getElementById('oauth-intro-success');
  107.     let sebox = document.getElementById('oauth-intro-error');
  108.     
  109.     aubox.hidden = true;
  110.     ssbox.hidden = true;
  111.     sebox.hidden = true;
  112.     sibox.hidden = false;
  113.     
  114.     let pas = document.getElementById('oauth-intro-phrase');
  115.     if (!Weave.Service.isLoggedIn) {
  116.       let uid = document.getElementById('oauth-intro-uid');
  117.       let pwd = document.getElementById('oauth-intro-pwd');
  118.       
  119.       Weave.OAuth.setUser(uid.value, pwd.value, pas.value);
  120.       Weave.Service.loginAndInit(null, uid.value, pwd.value, pas.value);
  121.     } else {
  122.       Weave.OAuth.setUser(Weave.Service.username, Weave.Service.password, pas.value);
  123.       Weave.Service.verifyPassphrase(null, Weave.Service.username, Weave.Service.password, pas.value);
  124.     }
  125.   },
  126.   
  127.   authorize: function OAuthWizard_authorize() {
  128.     document.getElementById('oauth-wizard').canAdvance = false;
  129.     document.getElementById('oauth-wizard').canRewind = false;
  130.     Weave.OAuth.validate(this._stringBundle, this._authUpdate);
  131.   },
  132.  
  133.   _authUpdate: function OAuthWizard__authUpdate(bundle, name, rsakey, conskey) {
  134.     document.getElementById('oauth-conf-loading').hidden = true;
  135.     
  136.     if (rsakey && conskey) {
  137.       Weave.OAuth._rsaKey = rsakey;
  138.       Weave.OAuth._consKey = conskey;
  139.     }
  140.  
  141.     if (name == "1" || name == "2" || name == "3" || name == "4") {
  142.       let err = document.getElementById('oauth-conf-error');
  143.       let ela = document.getElementById('oauth-conf-error-msg');
  144.       let msg = '';
  145.       switch (name) {
  146.         case "1":
  147.           msg = bundle.getString('conf.error1');
  148.           break;
  149.         case "2":
  150.           msg = bundle.getString('conf.error2');
  151.           break;
  152.         case "3":
  153.           msg = bundle.getString('conf.error3');
  154.           break;
  155.         case "4":
  156.           msg = bundle.getString('conf.error4');
  157.           break;
  158.       }
  159.       let fin = bundle.getFormattedString('conf.error', [msg]);
  160.       
  161.       ela.value = fin;
  162.       err.hidden = false;
  163.     } else {
  164.       let msg = document.getElementById('oauth-conf-msg');
  165.       msg.value = bundle.getFormattedString('conf.conmsg', [name]);
  166.       document.getElementById('oauth-conf-proceed').hidden = false;
  167.       document.getElementById('oauth-wizard').canAdvance = true;
  168.     }
  169.   },
  170.   
  171.   finalize: function OAuthWizard_finalize() {
  172.     document.getElementById('oauth-wizard').canRewind = false;
  173.     document.getElementById('oauth-wizard').canAdvance = false;
  174.     
  175.     Weave.OAuth.finalize(this._final1, this._final2, this._stringBundle);
  176.     document.getElementById('oauth-final-status').value = this._stringBundle.getString('final.step1');
  177.   },
  178.  
  179.   _final1: function OAuth__final1(bundle) {
  180.     document.getElementById('oauth-wizard').canAdvance = false;
  181.     document.getElementById('oauth-final-status').value = bundle.getString('final.step2');
  182.   },
  183.   
  184.   _final2: function OAuth__final2(bundle, succeded) {
  185.     if (succeded) {
  186.       document.getElementById('oauth-wizard').canAdvance = false;
  187.       document.getElementById('oauth-final-processing').hidden = true;
  188.       document.getElementById('oauth-final-status').value = bundle.getString('final.step3');
  189.     
  190.       if (Weave.OAuth._cback) {
  191.         window.location = Weave.OAuth._cback;
  192.       } else {
  193.         document.getElementById('oauth-final-manual').hidden = false;
  194.         document.getElementById('oauth-wizard').canAdvance = true;
  195.       }
  196.     }
  197.   },
  198.   
  199.   observe: function OAuth_observe(subject, topic, data) {
  200.     switch (topic) {
  201.       case "weave:service:verify-passphrase:success":
  202.         if (Weave.Service.isLoggedIn) {
  203.           this._loginDone(true);
  204.         } else {
  205.           Weave.Service.username = Weave.OAuth._uid;
  206.           Weave.Service.password = Weave.OAuth._pwd;
  207.           Weave.Service.login();
  208.         }
  209.         break;
  210.       case "weave:service:login:success":
  211.         this._loginDone(true);
  212.         break;
  213.       case "weave:service:verify-passphrase:error":
  214.         this._loginDone(false);
  215.         break;
  216.       case "weave:service:login:error":
  217.         this._loginDone(false);
  218.         break;
  219.     }
  220.   },
  221.   
  222.   _loginDone: function Oauth__loginDone(sure) {
  223.     let wizard = document.getElementById('oauth-wizard');
  224.     let aubox = document.getElementById('oauth-intro-aubox');
  225.     let sibox = document.getElementById('oauth-intro-athing');
  226.     let ssbox = document.getElementById('oauth-intro-success');
  227.     let sebox = document.getElementById('oauth-intro-error');
  228.     if (sure) {
  229.       aubox.hidden = true;
  230.       sibox.hidden = true;
  231.       sebox.hidden = true;
  232.       ssbox.hidden = false;
  233.       wizard.canAdvance = true;
  234.     } else {
  235.       aubox.hidden = false;
  236.       sibox.hidden = true;
  237.       sebox.hidden = false;
  238.       ssbox.hidden = true;
  239.       wizard.canAdvance = false;
  240.     }
  241.   }
  242. };
  243.  
  244. let gOAuthWizard = new OAuthWizard();
  245.